*-------------------------------------------------------------------------------
* Heterogenous Relative Government Spending Multipliers in the Era Surrounding the Great Recession
* Forthcoming in the Review of Economics and Statistics
* by Marco Bernardini, Selien De Schryder and Gert Peersman (fall 2018)
*
* This file generates figures 8 and 9
*-------------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
* 1) SETTINGS
////////////////////////////////////////////////////////////////////////////////
****************************
* housekeeping and loading *
****************************

* settings
clear all // clear memory 
cls // clear results window
set more off // do not display the "more" message
set graphics off // do (not) show graphics in STATA
adopath ++ PLUS


*********
* data *
********

clear
import excel "${path}figure_index.xlsx", sheet("Sheet1") firstrow clear
gen quarter = qofd(A)
format quarter %tq
drop A
order quarter,first
rename Ind_REC NRE
rename Ind_DEEP DRE
rename Ind_AG_REC CRE
rename IND_DEBT17 DEB

global begsample=2005 // beginning of the sample (2005q1 earliest)
global endsample=2015 // end of the sample (2015q4 latest)

local titNRE "recessions"
local titDRE "deep recessions"
local titCRE "continuous recession indicator"
local titDEB "high household debt"

local tA=tq(2005q1)
local tB=tq(2007q1)
local tC=tq(2009q1)
local tD=tq(2011q1)
local tE=tq(2013q1)
local tF=tq(2015q1)
local xtime `tA' "05Q1" `tB' "07Q1" `tC' "09Q1" `tD' "11Q1" `tE' "13Q1" `tF' "15Q1"
local start=tq(${begsample}q1)
local end=tq(${endsample}q4)

foreach case in NRE DRE CRE DEB {
	twoway (line `case' quarter, lpattern(solid) lwidth(thick) color(forest_green)), ///
	legend(off) name(fig_`case',replace) title("`tit`case''",size(medsmall)) ///
	graphregion(color(white)) plotregion(color(white)) ///
	xla(`xtime',grid labsize(small)) yla(,grid labsize(small)) ytitle("Size",size(medsmall))  xtitle("Time",size(medsmall)) //yla(-3(1)2.5,grid) ysc(r(-3 2.5))
}

graph combine fig_NRE fig_DRE fig_CRE fig_DEB, c(2) imargin(vsmall) name(final) ///
iscale(*1.0) ///
graphregion(color(white) margin(0 0 0 0)) plotregion(color(white) margin(0 0 0 0)) xsize(7) ysize(7)
graph export "${path}output\8_reshuffledata.pdf", replace
graph export "${path}output\8_reshuffledata.eps", replace
graph save "${path}output\8_reshuffledata.gph", replace

***************
* estimations *
***************

clear
import excel "${path}reshuffle.xlsx", sheet("Sheet1") firstrow clear

local titG_NRE "aggregate government spending" " "
local titG_DRE "aggregate government spending" " "
local titG_CRE "aggregate government spending" " "
local titG_DEB "aggregate government spending" " "

local titI_NRE "redistribution index" "(recessions)"
local titI_DRE "redistribution index" "(deep recessions)"
local titI_CRE "redistribution index" "(continuous recession indicator)"
local titI_DEB "redistribution index" "(high household debt)"

local titY_NRE "aggregate GDP" " "
local titY_DRE "aggregate GDP" " "
local titY_CRE "aggregate GDP" " "
local titY_DEB "aggregate GDP" " "

local laG "-0.4(0.1)0.4"
*local laI "-0.05(0.02)0.13"
local laY "-0.4(0.2)0.8"
local raG "ra(-0.4 0.4)"
*local raI "ra(-0.05 0.15)"
local raY "ra(-0.4 0.8)"

local v1 "G"
local v2 "I"
local v3 "Y"

local c1 "NRE"
local c2 "DRE"
local c3 "CRE"
local c4 "DEB"

foreach res in 1 2 3 {
	foreach case in 1 2 3 4 {
		twoway (rarea IRF`case'4`res' IRF`case'5`res' h, ///
				fcolor(gs14) lcolor(gs13) lpattern(solid)) ///
			   (rarea IRF`case'2`res' IRF`case'3`res'  h, ///
				fcolor(gs10) lcolor(gs10) lpattern(solid)) ///
				(line IRF`case'1`res' h, lpattern(solid) lwidth(thick) color(midblue)) ///				
				(pcarrowi 0 0 0 8, lcolor(black) lpattern(shortdash) mcolor(black) msize(zero)), ///
				legend(off) name(fig_`v`res''_`c`case'',replace) title("`tit`v`res''_`c`case'''",size(small)) ///
				graphregion(color(white)) plotregion(color(white)) ///
				xla(0(1)8,grid labsize(small)) yla(`la`res'',grid labsize(small)) ysc(`ra`res'') ytitle("Size",size(small))  xtitle("Horizon",size(small)) 
	}
}

graph combine fig_G_NRE fig_I_NRE fig_Y_NRE, c(3) imargin(vsmall) name(finalA) ///iscale(*.8) /// 
graphregion(color(white) margin(0 5 0 0)) plotregion(color(white) margin(zero)) ///
title("(a) redistribution of spending to US states {it:in recessions}",size(small))

graph combine fig_G_DRE fig_I_DRE fig_Y_DRE, c(3) imargin(vsmall) name(finalB) ///iscale(*.8) /// 
graphregion(color(white) margin(0 5 0 0)) plotregion(color(white) margin(zero)) ///
title("(b) redistribution of spending to US states {it:in deep recessions}",size(small))

graph combine fig_G_CRE fig_I_CRE fig_Y_CRE, c(3) imargin(vsmall) name(finalC) ///iscale(*.8) /// 
graphregion(color(white) margin(0 5 0 0)) plotregion(color(white) margin(zero)) ///
title("(c) redistribution of spending to US states {it:in recessions (continuous indicator)}",size(small))

graph combine fig_G_DEB fig_I_DEB fig_Y_DEB, c(3) imargin(vsmall) name(finalD) ///iscale(*.8) /// 
graphregion(color(white) margin(0 5 0 0)) plotregion(color(white) margin(zero)) ///
title("(d) redistribution of spending to US states {it:with high household debt}",size(small))

graph combine finalA finalB finalC finalD, r(4) imargin(vsmall) ///
graphregion(color(white) margin(0 5 0 0)) plotregion(color(white) margin(zero)) xsize(7) ysize(9) ///

graph export "${path}output\9_reshuffle.pdf", replace
graph export "${path}output\9_reshuffle.eps", replace
graph save "${path}output\9_reshuffle.gph", replace
